package com.ltf.config;

import com.ltf.entity.XUser;
import com.ltf.service.XUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {
    //打印输出换成这个
    private Logger logger=LoggerFactory.getLogger(this.getClass());
    @Autowired
    XUserService xUserService;
    @Override
    //user 浏览器上登录窗口输入的账号 v-model="username"
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.debug("loadUserByUsername-网页输入的账号："+username);
        XUser m = xUserService.findByPhone(username);
        logger.debug("返回的用户："+m);
        //发生向上转型  Granted 授予 Authority 权限
        GrantedAuthority p1=new SimpleGrantedAuthority((String) m.getRole());// 权限1
//        GrantedAuthority p2=new SimpleGrantedAuthority("role");// 权限2
        List<GrantedAuthority> lst =new ArrayList<>();
        lst.add(p1);
//        lst.add(p2);
        //提供 UserDetails 的 实现类 User
        //org.springframework.security.core.userdetails.User;
       UserDetails u= new User(username,(String) m.getPwd(),lst);
        return u;
    }

}

//        System.out.println("怎么比对加密结果是由111加密产生");
//                System.out.println(p2.matches("111",s1));